package com.solocredit.finance.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.solocredit.finance.R;

/**
 * 虚线效果
 */
public class DashedLineView extends View {

    private Paint paint;
    private Path path;
    private int lineColor = Color.BLACK;
    private float strokeWidth = 4f;
    private float dashLength = 10f;
    private float gapLength = 5f;
    private boolean isHorizontal = true;

    public DashedLineView(Context context) {
        super(context);
        init();
    }

    public DashedLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DashedLineView);

        lineColor = a.getColor(R.styleable.DashedLineView_lineColor, Color.BLACK);
        strokeWidth = a.getDimension(R.styleable.DashedLineView_strokeWidth, 4f);
        dashLength = a.getDimension(R.styleable.DashedLineView_dashLength, 10f);
        gapLength = a.getDimension(R.styleable.DashedLineView_gapLength, 5f);
        int orientation = a.getInt(R.styleable.DashedLineView_orientation, 0);
        isHorizontal = (orientation == 0);
        a.recycle();

        init();
    }

    public DashedLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(lineColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);
        paint.setPathEffect(new DashPathEffect(new float[]{dashLength, gapLength}, 0));

        path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isHorizontal) {
            // 水平虚线
            path.moveTo(0, getHeight() / 2f);
            path.lineTo(getWidth(), getHeight() / 2f);
        } else {
            // 垂直虚线
            path.moveTo(getWidth() / 2f, 0);
            path.lineTo(getWidth() / 2f, getHeight());
        }

        canvas.drawPath(path, paint);
        path.reset();
    }

    // 设置虚线颜色
    public void setLineColor(int color) {
        this.lineColor = color;
        paint.setColor(color);
        invalidate();
    }

    // 设置虚线宽度
    public void setStrokeWidth(float width) {
        this.strokeWidth = width;
        paint.setStrokeWidth(width);
        invalidate();
    }

    // 设置虚线样式
    public void setDashPattern(float dashLength, float gapLength) {
        this.dashLength = dashLength;
        this.gapLength = gapLength;
        paint.setPathEffect(new DashPathEffect(new float[]{dashLength, gapLength}, 0));
        invalidate();
    }

    // 设置方向 (水平/垂直)
    public void setOrientation(boolean isHorizontal) {
        this.isHorizontal = isHorizontal;
        invalidate();
    }
}
